<template>
  <!-- #ifndef MP-TOUTIAO -->
  <custom-nav-bar title="榜单"></custom-nav-bar>
  <!-- #endif -->
  <view class="content">
    <!-- 导航栏 -->
    <view class="navbar">
      <view :class="['nav-item', { active: activeTab === 'album' }]" @click="switchTab('album')"
        >专辑榜</view
      >
      <view :class="['nav-item', { active: activeTab === 'wall' }]" @click="switchTab('wall')"
        >壁纸榜</view
      >
    </view>

    <!-- 专辑榜 -->
    <view class="album-list" v-if="activeTab === 'album'">
      <view
        class="album-item"
        v-for="item in albumList"
        :key="item.id"
        @click="goPreview1(item.id)"
      >
        <view class="rank">{{ item.rank }}</view>
        <image :src="item.imgUrl" mode="aspectFill"></image>
        <view class="nickname">著：{{ item.nickname }}</view>
      </view>
    </view>

    <!-- 壁纸榜 -->
    <view class="wall-list" v-if="activeTab === 'wall'">
      <view class="wall-item" v-for="item in wallList" :key="item.id" @click="goPreview2(item.id)">
        <view class="rank">{{ item.rank }}</view>
        <image :src="item.smallPicurl" mode="aspectFill"></image>
        <!-- 其他可能的数据显示 -->
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { apiGetWallRank, apiGetAlbumRank } from '@/api/apis.js'
import CustomNavBar from '@/components/custom-nav-bar/custom-nav-bar.vue'

// 数据状态
const activeTab = ref('album')

// 方法
const switchTab = (tab) => {
  activeTab.value = tab
}
const params = {
  page: 1,
  limit: 10,
}

const albumList = ref([]) // 新增专辑榜列表
const getAlbumRank = async () => {
  try {

    let res = await apiGetAlbumRank(params)
    console.log(res)
    if (res && res.rows) {
      albumList.value = res.rows.sort((a, b) => a.rank - b.rank)
    }
  } catch (error) {
    console.error('API请求失败:', error)
  }
}

const wallList = ref([])
const getWallRank = async () => {
  try {
    let res = await apiGetWallRank(params)
    console.log(res)
    if (res && res.rows) {
      // 根据 rank 字段进行排序
      wallList.value = res.rows.sort((a, b) => a.rank - b.rank)
    } else {
      console.error('API响应数据格式不正确')
    }
  } catch (error) {
    console.error('API请求失败:', error)
  }
}

onMounted(() => {
  getWallRank()
  getAlbumRank() // 获取专辑榜数据
})
//专辑跳转到预览页面
const goPreview1 = (id) => {
  uni.navigateTo({
    url: '/pages/preview/previewAlbum?id=' + id,
  })
}
//壁纸跳转到预览页面
const goPreview2 = (id) => {
  uni.navigateTo({
    url: '/pages/preview/preview?id=' + id,
  })
}
</script>

<style lang="scss" scoped>
.content {
  padding: 20rpx;
  height: 100vh; // 让内容区域占据整个屏幕高度
  overflow-y: auto; // 允许垂直滚动

  .navbar {
    display: flex;
    justify-content: space-around;
    margin-bottom: 20rpx;

    .nav-item {
      font-size: 32rpx;
      color: #999;
      padding: 10rpx;

      &.active {
        color: #333;
        border-bottom: 4rpx solid #333;
      }
    }
  }
  .wall-list {
    padding: 30rpx;
    display: grid;
    grid-template-columns: repeat(3, 1fr); // 每行三列
    gap: 15rpx;
    max-width: 100%; // 最大宽度为100%
    width: 100%; // 宽度为100%

    .wall-item {
      width: 100%;
      padding-bottom: 200%; // 保持图片宽高比为1:2
      position: relative;

      .rank {
        position: absolute;
        top: 10rpx;
        left: 10rpx;
        background-color: rgba(0, 0, 0, 0.5); // 背景颜色稍微透明一些，以确保数字清晰可见
        color: white; // 文字颜色为白色
        padding: 5rpx 10rpx; // 给数字周围留一点空间
        border-radius: 5rpx; // 圆角边框
        font-size: 28rpx; // 字体大小
        z-index: 1; // 确保排名数字位于图片之上
      }

      image {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        object-fit: cover; // 图片覆盖整个区域
      }
    }
  }
  .album-list {
    padding: 30rpx;
    display: grid;
    grid-template-columns: repeat(2, 1fr); // 每行两列，根据需要调整
    gap: 15rpx;
    max-width: 100%; // 最大宽度为100%
    width: 100%; // 宽度为100%

    .album-item {
      width: 100%;
      padding-bottom: 150%; // 保持图片宽高比为1:1.5，根据需要调整
      position: relative;

      .rank {
        position: absolute;
        top: 10rpx;
        left: 10rpx;
        background-color: rgba(0, 0, 0, 0.5);
        color: white;
        padding: 5rpx 10rpx;
        border-radius: 5rpx;
        font-size: 28rpx;
        z-index: 1;
      }

      image {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        object-fit: cover;
      }

      .nickname {
        position: absolute;
        bottom: 10rpx;
        left: 10rpx;
        background-color: rgba(0, 0, 0, 0.5);
        color: white;
        padding: 5rpx 10rpx;
        border-radius: 5rpx;
        font-size: 24rpx;
        z-index: 1;
      }
    }
  }
}
</style>
